<template>
    <div class="default-main ba-table-box">
        <el-alert class="ba-table-alert" v-if="baTable.table.remark" :title="baTable.table.remark" type="info" show-icon />

        <div style="background: #fff; padding: 13px 15px;">
            <span :class="[state.type == '' ? 'btn btn-info' : 'btn']" @click="changeType('')">{{ t('全部') }}</span>
            <span :class="[state.type == '出口' ? 'btn btn-info ml-12' : 'btn ml-12']" @click="changeType('出口')">{{ t('出口') }}</span>
            <span :class="[state.type == '进口' ? 'btn btn-info ml-12' : 'btn ml-12']" @click="changeType('进口')">{{ t('进口') }}</span>
            <span :class="[state.type == '内销' ? 'btn btn-info ml-12' : 'btn ml-12']" @click="changeType('内销')">{{ t('内销') }}</span>
        </div>

        <!-- 表格顶部菜单 -->
        <!-- 自定义按钮请使用插槽，甚至公共搜索也可以使用具名插槽渲染，参见文档 -->
        <TableHeader :buttons="['add', 'delete', 'quickSearch', 'columnDisplay']" :quick-search-placeholder="t('搜索')">
            
        </TableHeader>

        <!-- 表格 -->
        <!-- 表格列有多种自定义渲染方式，比如自定义组件、具名插槽等，参见文档 -->
        <!-- 要使用 el-table 组件原有的属性，直接加在 Table 标签上即可 -->
        
        <Table ref="tableRef">

            <template #customer>
                <!-- 在插槽内，您可以随意发挥，通常使用 el-table-column 组件 -->
                <el-table-column :label="t('客户名')" align="center" width="100">
                    <template #default="scope">
                        <div>{{ scope.row.customer_info.corp_name_ko }}</div>
                    </template>
                </el-table-column>
            </template>

            <template #type>
                <!-- 在插槽内，您可以随意发挥，通常使用 el-table-column 组件 -->
                <el-table-column :label="t('类型')" align="center" width="120">
                    <template #default="scope">
                        {{ scope.row.type }}({{ t(scope.row.trade_type) }})
                    </template>
                </el-table-column>
            </template>

            <template #detail>
                <!-- 在插槽内，您可以随意发挥，通常使用 el-table-column 组件 -->
                <el-table-column :label="t('订单明细')" align="center" width="100">
                    <template #default="scope">
                        <el-link v-if="!scope.row.is_detail">导入</el-link>
                        <el-link v-if="scope.row.is_detail">查看</el-link>
                    </template>
                </el-table-column>
            </template>
            
            
        </Table>

        <!-- 表单 -->
        <PopupForm />

        <OrderList v-if="state.clickRightId > 0" :order_id="state.clickRightId" @hideOrderList="hideOrderList"  />

    </div>
</template>

<script setup lang="ts">
import { ref, provide, onMounted, reactive } from 'vue'
import baTableClass from '/@/utils/baTable'
import { defaultOptButtons } from '/@/components/table'
import { baTableApi } from '/@/api/common'
import { useI18n } from 'vue-i18n'
import PopupForm from './popupForm.vue'
import Quotation from './quotation/index.vue'
import Table from '/@/components/table/index.vue'
import TableHeader from '/@/components/table/header/index.vue'

import OrderList from './list/index.vue';




defineOptions({
    name: 'order/customer/index',
})

const { t } = useI18n()
const tableRef = ref()




const state = reactive({
    type: '',
    clickRightId: 0,
});

const handleRowContextmenu = (id: number) => {
    state.clickRightId = id;
}

const hideOrderList = () => {
    state.clickRightId = 0;
}




let optButtons = defaultOptButtons([])


// 自定义一个新的按钮
let newButton: OptButton[] = [
    // {
    //     render: 'basicButton', // 渲染方式:tipButton=带tip的按钮,confirmButton=带确认框的按钮,moveButton=移动按钮
    //     name: 'mx',
    //     title: '',
    //     text: t('订单明细'),
    //     class: 'btn btn-default btn-small',
    //     type: '', // 按钮类型，请参考 element plus 的按钮类型
    //     icon: '', // 按钮 icon
    //     disabledTip: true, // tipButton 禁用 tip
    //     click: (row: TableRow, field: TableColumn) => { // 自定义点击事件
    //         handleRowContextmenu(row.id)
    //     },
    //     display: (row: TableRow, field: TableColumn) => { // 按钮是否显示，请返回布尔值
    //         return true;
    //     },
    //     disabled: (row: TableRow, field: TableColumn) => { // 按钮是否禁用，请返回布尔值
    //         return false;
    //     },
    // },
    {
        render: 'basicButton', // 渲染方式:tipButton=带tip的按钮,confirmButton=带确认框的按钮,moveButton=移动按钮
        name: 'edit',
        title: 'edit',
        text: t('编辑'),
        type: 'primary',
        class: 'btn btn-primary btn-small',
        icon: '',
    },{
        render: 'confirmButton',
        name: 'delete',
        title: '',
        text: t('删除'),
        type: 'danger',
        class: 'btn btn-danger mt-0 btn-small',
        icon: '',
        popconfirm: {
            confirmButtonText: t('Delete'),
            cancelButtonText: t('Cancel'),
            confirmButtonType: 'danger',
            title: t('Are you sure to delete the selected record?'),
        }
    }
]

// 新按钮合入到默认的按钮数组
optButtons = newButton.concat(optButtons)


// 创建一个新的 Date 对象来获取当前日期
const currentDate = new Date();

// 获取年、月和日
const year = currentDate.getFullYear();
const month = String(currentDate.getMonth() + 1).padStart(2, '0'); // 月份是从 0 开始计数，所以要加 1
const day = String(currentDate.getDate()).padStart(2, '0');

// 组合成 YYYY-MM-DD 格式的日期字符串
const formattedDate = `${year}-${month}-${day}`;


/**
 * baTable 内包含了表格的所有数据且数据具备响应性，然后通过 provide 注入给了后代组件
 */
const baTable = new baTableClass(
    new baTableApi('/system/order.Customer/'),
    {
        pk: 'id',
        column: [
            { type: 'selection', align: 'center', operator: false },
            { label: t('订单号'), prop: 'out_trade_no', align: 'center', width: 90 }, 
            { label: t('客户名'), prop: 'id', render: 'slot', slotName: 'customer' },
            { label: t('类型'), prop: 'id', render: 'slot', slotName: 'type' },
            { label: t('订单明细'), prop: 'detail', render: 'slot', slotName: 'detail' }, 
            { label: t('订单总价'), prop: 'amount', align: 'center' }, 
            { label: t('付款金额'), prop: 'amount', align: 'center' }, 
            { label: t('状态'), prop: 'amount', align: 'center' }, 
            { label: t('备注'), prop: 'remark', align: 'center' },
            { label: t('下单时间'), prop: 'create_time', align: 'center', width: 100 },
            
            { label: t('操作'), align: 'center', render: 'buttons', buttons: optButtons, operator: false, width: 200 },
        ],
        dblClickNotEditColumn: ['all'],
        defaultOrder: { prop: 'create_time', order: 'desc' },
    },
    {
        defaultItems: { create_time: formattedDate },
    },{
        
    },{
        getIndex: ({ res }: { res: ApiResponse }) => { // 查看后

        }
    }
)

provide('baTable', baTable)

onMounted(() => {
    baTable.table.ref = tableRef.value
    baTable.mount()
    baTable.getIndex()?.then(() => {
        baTable.initSort()
        baTable.dragSort()
    })
})
</script>

<style scoped lang="scss">
:deep(.ba-operate-dialog .el-dialog__body){
    height: 300px;
}


:deep(.ba-operate-dialog-list .el-dialog__body){
    height: calc(95vh - 56px);
    padding-left: 0;
    padding-right: 0;
}
:deep(.ba-operate-dialog-list){
    margin-top: 2.5vh;
    width: 1200px;
    max-width: 1500px;
}

:deep(.ba-operate-dialog-list-add .el-dialog__body){
    height: calc(30vh);    
}

</style>
